package cc.telecomdigital.tdstock.adapter;

import java.util.List;

import cc.telecomdigital.tdstock.R;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class StockSearchListItemAdapter extends BaseAdapter
{
	private LayoutInflater				minflater;
	private List<StockSearchViewHolder>	list;

	public StockSearchListItemAdapter(LayoutInflater inflater,
			List<StockSearchViewHolder> list)
	{
		super();
		this.minflater = inflater;
		this.list = list;
	}

	@Override
	public int getCount()
	{
		// TODO Auto-generated method stub
		return this.list.size();
	}

	@Override
	public Object getItem(int position)
	{
		return position < this.list.size() ? this.list.get(position) : null;
	}

	@Override
	public long getItemId(int position)
	{
		return position < this.list.size() ? this.list.get(position).id : -1l;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		StockSearchViewHolder commHolder = null;
		if (convertView == null)
		{
			convertView = minflater
					.inflate(R.layout.search_stock_item, null);
			commHolder = new StockSearchViewHolder();
			commHolder.item = (TextView) convertView.findViewById(R.id.item);

			convertView.setDrawingCacheBackgroundColor(Color.WHITE);

			//synchronize with iphone

			convertView.setTag(commHolder);
		}
		else
		{
			commHolder = (StockSearchViewHolder) convertView.getTag();
		}
		if (position + 1 > this.list.size())
		{
			return convertView;
		}

		commHolder.item.setText(this.list.get(position).item.getText());
		if (position == this.list.size() - 1)
		{
			commHolder.item
					.setTextColor(this.list.get(position).searchState == 0
							|| this.list.get(position).searchState == 1 ? Color.BLUE
							: Color.RED);
		}
		else
		{
			commHolder.item.setTextColor(Color.BLACK);
		}
		return convertView;
	}

}
